import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:thinking/config/_.dart';
import 'package:thinking/entity/_.dart';
import 'package:thinking/event/ClassEditEvent.dart';
import 'package:thinking/service/_.dart';
import 'package:thinking/utils/_.dart';
import 'package:thinking/widget/_.dart';

class EditDiaLog extends StatefulWidget{

  @override
  _EditDiaLog createState() => _EditDiaLog();

}

class _EditDiaLog extends State<EditDiaLog>{

  List<ClassData> principal = [];
  List<ClassData> additional = [];
  bool isEdit = false;


    @override
  void initState()  {
    super.initState();
    getList();
  }

  void getList() async{
      var a = await ClassService.findAll('Principal');
      var b = await ClassService.findAll('Additional');
      setState(() {
        principal = a.map((e) => ClassData.fromJson(e)).toList();
        additional = b.map((e) => ClassData.fromJson(e)).toList();
      });

  }

  Future<void> principalClick(ClassData item) async{
      if(item.id == 1 || item.id == 2){
        BotToast.showText(text: '${item.title}标签不可删除');
        return;
      }
      principal = principal.where((v) => v.id != item.id).toList();
      additional.add(item);
      this.isEdit = true;
      var loading = BotToast.showLoading();
      await ClassService.add(tableName: 'Additional',item: item);
      await ClassService.delete(tableName:'Principal',item: item);
      loading();
      setState(() {
      });

  }

  Future<void> additionalClick(ClassData item) async {

    additional = additional.where((v) => v.id != item.id).toList();
    principal.add(item);
    this.isEdit = true;
    var loading = BotToast.showLoading();
    await ClassService.add(tableName: 'Principal',item: item);
    await ClassService.delete(tableName:'Additional',item: item);
    loading();
    setState(() {
    });

  }

    @override
  Widget build(BuildContext context) {
          return WillPopScope(
            child:Padding(
              padding: EdgeInsets.all(12),
              child: Material(
                type: MaterialType.transparency,
                child: Container(
                  decoration: ShapeDecoration(
                      color: Color(0xFFFFFFFF),
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.all(Radius.circular(20))
                      )

                  ),
                  margin: EdgeInsets.fromLTRB(10, Screen.setHeight(100), 10, Screen.setHeight(100)),
                  padding: EdgeInsets.all(16),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text('编辑分类',style: TextStyle(color:ColorConfig.ThemeColor,fontSize: 18,fontWeight: FontWeight.w600 )),
                      Expanded(
                        child: Container(
                          padding: EdgeInsets.all(4),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              SizedBox(
                                height: 5,
                              ),
                              Text('已选菜单',style: TextStyle(color:ColorConfig.ThemeObsColor,fontSize: 15,fontWeight: FontWeight.w600  )),
                              Container(
                                height: Screen.setHeight(120),
                                child: ListView(
                                  physics:BouncingScrollPhysics() ,
                                  children: [
                                    Wrap(
                                      spacing: Screen.setFontSize(13),
                                      children:
                                      principal.map((e) =>
                                          EditItem(
                                              key: ValueKey(e.id),
                                              val: e.title!,
                                              callBack: (){
                                                principalClick(e);
                                              })
                                      ).toList(),

                                    )
                                  ],
                                ),
                              ),
                              Text('未选菜单',style: TextStyle(color:ColorConfig.ThemeObsColor,fontSize: 15,fontWeight: FontWeight.w600  )),
                              Container(
                                height: Screen.setHeight(150),
                                child: ListView(
                                  physics:BouncingScrollPhysics() ,
                                  children: [
                                    Wrap(
                                      spacing: Screen.setFontSize(10),
                                      children:
                                      additional.map((e) =>
                                          EditItem(
                                              key: ValueKey(e.id),
                                              val: e.title!,
                                              callBack: (){
                                                  additionalClick(e);
                                              })
                                      ).toList(),

                                    )
                                  ],
                                ),
                              ),
                              SizedBox(
                                height: Screen.setHeight(20),
                              ),

                            ],

                          ),
                        ),
                      )
                    ],
                  ),
                ),
              ),
            ) ,
            onWillPop: () async{
              Config.eventBus.fire(ClassEditEvent(isEdit,principal.length));
              return true;
            },
          );

  }

}